/**
 * Copyright (c) 2010 Sense Tecnic Systems Inc.
 */
package com.testcase.vincentApp;

import com.testcase.R;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;

/**
 * Event handler to handle when a user preference is changed.
 * 
 * @author Vincent Tsao
 */
public class PreferenceChangedHandler implements OnSharedPreferenceChangeListener
{
	private Resources resources;
	private SensorManager sensorManager;

	/**
	 * Constructor to create a new instance of this preference changed event
	 * handler.
	 * 
	 * @param resources
	 * @param sensorManager
	 */
	public PreferenceChangedHandler (Resources resources, SensorManager sensorManager)
	{
		this.resources = resources;
		this.sensorManager = sensorManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeandroid.content.SharedPreferences.OnSharedPreferenceChangeListener#
	 * onSharedPreferenceChanged(android.content.SharedPreferences,
	 * java.lang.String)
	 */
	public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key)
	{
		String isActiveKey = resources.getString (R.string.isActiveKey);
		String updateIntervalKey = resources.getString (R.string.updateIntervalKey);
		boolean isActive = sharedPreferences.getBoolean (isActiveKey, true);

		if (key.equals (isActiveKey))
		{
			if (isActive)
				sensorManager.startSensorUpdates ();
			else
				sensorManager.stopSensorUpdates ();
		}
		else if (key.equals (updateIntervalKey))
		{
			if (isActive)
				sensorManager.startSensorUpdates ();
		}
	}
}
